package boogui.seat;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.BoxLayout;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

public class MemberSeat4 extends JDialog {

	private BooguiSeatDAO bdao = new BooguiSeatDAO();

	private JCheckBox seats[][];
	private JCheckBox seats2[][];
	private JCheckBox seatsno[];
	private JLabel label[];

	private char isSelec;
	
	private String isSelect;
	private String isSelect2;

	private static String updateSeatSQL;

	private final JPanel contentPanel = new JPanel();
	
	int as = 0;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			MemberSeat4 dialog = new MemberSeat4();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public MemberSeat4() {
		setBounds(100, 100, 500, 500);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.NORTH);
			panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
			{
				JPanel panel_1 = new JPanel();
				panel.add(panel_1);
				{
					JLabel lblNewLabel = new JLabel("Screen");
					lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 30));
					panel_1.add(lblNewLabel);
				}
			}
			{
				JPanel panel_1 = new JPanel();
				panel.add(panel_1);

				label = new JLabel[18];

				label[0] = new JLabel();
				panel_1.add(label[0]);
				panel_1.setLayout(new GridLayout(1, 18, 0, 0));
				label[0].setIcon(new ImageIcon(MemberSeat4.class
						.getResource("/images/seat/blank.png")));
				label[0].setEnabled(false);
				//위 숫자 나열
				for (int i = 1; i < 18; i++) {
					if (i == 5 || i == 13) {	//비울칸을 지정해주고 png 파일을 넣어줌
						label[i] = new JLabel();
						panel_1.add(label[i]);
						label[i].setIcon(new ImageIcon(MemberSeat4.class
								.getResource("/images/blank.png")));

					} else {

						label[i] = new JLabel();
						panel_1.add(label[i]);
						label[i].setIcon(new ImageIcon(MemberSeat4.class
								.getResource("/images/seat/" + i + ".png")));
					}
				}
			}
			{
				JPanel panel2 = new JPanel();
				contentPanel.add(panel2, BorderLayout.WEST);
				label = new JLabel[13];
				panel2.setLayout(new GridLayout(12, 0, 5, 0));
				//왼쪽 영어 이미지
				for (int i = 1; i <= 12; ++i) {
					label[i] = new JLabel();
					panel2.add(label[i]);
					label[i].setIcon(new ImageIcon(MemberSeat4.class
							.getResource("/images/seat/seatab/" + i + ".png")));
				}

			}
			{
				JPanel panel3 = new JPanel();
				contentPanel.add(panel3, BorderLayout.CENTER);
				seats = new JCheckBox['M'][18];
				//체크박스 나열
				for (int c = 'A'; c < 'M'; c++) {	//열처리
					for (int i = 1; i < 18; i++) {	//행처리
						if (i == 5 || i == 13) {	//비울칸 지정및 png 파일 넣어줌
							seats[c][i] = new JCheckBox();
							panel3.add(seats[c][i]);
							seats[c][i].setIcon(new ImageIcon(MemberSeat4.class
									.getResource("/images/blank.png")));

						} else {
							
							seats[c][i] = new JCheckBox();	//비울칸 제외한 곳에 체크박스를 넣어줌
							panel3.add(seats[c][i]);
							as += 1;
							System.out.println(as);
						}
					}
				}
			}
			
			{
				JPanel buttonPane = new JPanel();
				buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
				getContentPane().add(buttonPane, BorderLayout.SOUTH);
				{
					JButton okButton = new JButton("예매하기");
					okButton.addActionListener(new ActionListener() {
						//여기부터 체크박스에서 체크해서 예매하기를 눌렀을 때 누른 배열 DB에다가 X값을 넣어줌
						public void actionPerformed(ActionEvent e) { 

							for (char c = 'A'; c < 'M'; c++) {
								for (int i = 1; i < 18; ++i) {
									if (i == 5 || i == 13) {

									} else {
										if (seats[c][i].isSelected()) {
											seats[c][i].setEnabled(false);
											isSelec = 'X';
											updateSeatSQL = "update seatinfo set "
													+ c + i 
													+ "='" + isSelec + "' where showno = 551";
											System.out.println(isSelec);System.out.println(updateSeatSQL);
											updateSeat();
										}
									}
								}//안 for
							}//바깥 for ㄲ
						}
					});
					isSelect = "X";
					
					
					//System.out.println(bdao.selectSeat(551).size());
					List<String> seatinfo =  bdao.selectSeat(551);//여기부터 DB 값을 체크박스로 바꿔주기
					System.out.println(seatinfo.get(0));	
					int cnt = 0;
					for (char c = 'A'; c < 'M'; c++) {
						for (int i = 1; i < 18; ++i) {
							if (i == 5 || i == 13) {

							} else {
								if (seatinfo.get(cnt).equals("X")) {
									seats[c][i].setSelected(true);
								}
								cnt++;
							}
						}//안 for
					}//바깥 for
					//여기까지
					
					okButton.setActionCommand("OK");
					buttonPane.add(okButton);
					getRootPane().setDefaultButton(okButton);
				}
				
				{
					JButton cancelButton = new JButton("예매하지 않고 닫기");
					cancelButton.setActionCommand("Cancel");
					buttonPane.add(cancelButton);
				}
			}
		}
	}
	
	/*public void readMember() {

		BooguiSeatDAO bsdao = new BooguiSeatDAO();

		for (MemberSeat4 m : bsdao.selectSeat()) {

			List<MemberSeat4> results = new ArrayList<MemberSeat4>();

			for (int c = 0; c < 12; c++) {
				for (int i = 0; i < 17; i++) {
					if (i == 4 || i == 12) {

					} else {
							for(bsdao.selectSeat().equals("O")){
								
							}
						

					}
				}
			}

		}

	}*/

	public boolean updateSeat() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		BooguiSeatDAO bdao = new BooguiSeatDAO();
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(updateSeatSQL);
			pstmt.executeUpdate();
			result = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}
		return result;
	}

	/*
		public void modifyMember() {
		
		readMember(); MemberSeat4 ms4 = new MemberSeat4();
		Scanner sc = new Scanner(System.in);
	
		if(mdao.update(mvo)) System.out.println("회원정보수정 성공");
		else System.out.println("회원정보수정 실패 꺼졍"); }
	 */
}
